/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchdantocException;
import doan.quanlycanbo.portlet.model.dantoc;
import doan.quanlycanbo.portlet.service.base.dantocLocalServiceBaseImpl;

/**
 * 
 * @author Nguyen Tuan Vinh
 */
public class dantocLocalServiceImpl extends dantocLocalServiceBaseImpl {
	
	public List<dantoc> getAll() throws SystemException{
		return dantocPersistence.findAll();
	}
	
	public dantoc addDanhToc(dantoc dantoc1, long userId) throws SystemException{
		long macapuy = CounterLocalServiceUtil.increment(dantoc.class.getName());
		dantoc dantoc2 = dantocPersistence.create(macapuy);
		dantoc2.setGroupId(dantoc1.getGroupId());
		dantoc2.setCompanyId(dantoc1.getCompanyId());
		dantoc2.setLanguageId(dantoc1.getLanguageId());
		dantoc2.setTendantoc(dantoc1.getTendantoc());
		
		try {
			resourceLocalService.addResources(dantoc1.getCompanyId(), dantoc1.getGroupId(), userId, 
					dantoc.class.getName(), dantoc2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return dantocPersistence.update(dantoc2, false);
	}
	
	public dantoc updateDantoc(long madantoc, String tendantoc) throws SystemException, NoSuchdantocException{
		dantoc danToc = dantocPersistence.findByPrimaryKey(madantoc);
		danToc.setTendantoc(tendantoc);
		return dantocPersistence.update(danToc, false);
	}
	
	public dantoc deleteDantoc(long madantoc) throws SystemException, NoSuchdantocException,PortalException{
		dantoc danToc = dantocPersistence.findByPrimaryKey(madantoc);
		return deleteDantoc(danToc);
	}
	
	public dantoc deleteDantoc(dantoc danToc) throws SystemException,PortalException{
		resourceLocalService.deleteResource(danToc.getCompanyId(), dantoc.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, danToc.getPrimaryKey());
		return dantocPersistence.remove(danToc);
	}
}